package com.kgc.onlineLife.service.impl;

import com.kgc.onlineLife.bean.Order;
import com.kgc.onlineLife.bean.OrderItem;
import com.kgc.onlineLife.bean.Product;
import com.kgc.onlineLife.mapper.OrderItemMapper;
import com.kgc.onlineLife.mapper.OrderMapper;
import com.kgc.onlineLife.service.OrderItemService;
import com.kgc.onlineLife.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: com.kgc.onlineLife.service.impl
 * @author: HarryXzy
 * @create: 2022-04-18 22:29
 * @description: 订单service
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
    * @author: HarryXzy
    * @date: 2022/4/18 10:40 下午
    * @return:
    * @description: 根据用户id查询订单
    **/
    @Override
    public List<Order> myOrders(Integer id) {
        return orderMapper.selectOrdersByCstid(id);
    }

    /**
    * @author: HarryXzy
    * @date: 2022/4/18 10:43 下午
    * @return:
    * @description: 给每个订单的订单项设置属性值，orderitems、total等
    **/
    @Override
    public void fill(List<Order> os) {
        for (Order o :os){
            //遍历用户的每一个订单
            fill(o);
        }
    }

    public void fill(Order o) {
        //查出当前订单下的所有订单项
        List<OrderItem> ois = orderItemMapper.selectByOid(o.getId());
        //订单总金额
        float total=0;
        for(OrderItem oi:ois){
            total+=oi.getNumber()*oi.getProduct().getPrice();
        }
        o.setTotal(total);
        o.setOrderItems(ois);
    }

    @Override
    public boolean add(Order order) {
        return orderMapper.insertOrder(order) > 0;
    }

    @Override
    public void update(Order order) {
        orderMapper.updateOrder(order);
    }

    @Override
    public boolean insert(Order order) {
        return orderMapper.insertOrder(order) > 0;
    }

    @Override
    public int selectOrderByCode(String code) {
        return orderMapper.selectOrderByCode(code);
    }

/**
 * @author : wangzhongyu
 * @date   : 2022/4/21
 * @param  :
 * @return :
 * @description :
 */
@Override
public List<Order> queryOrder() {
    return orderMapper.selectOrder();
}

    @Override
    public List<Order> queryOrder(String code) {
        return orderMapper.selectOrders(code);
    }

    @Override
    public Boolean modOrder(String pname) {
        return orderMapper.upOrder(pname);
    }


}
